//void, Obj This

Settlement set;

Sleep(800+rand(GetConst("RandTimeStart"))+GetConst("AddTimeStart"));

set = This.AsBuilding().settlement;

if (This.AsBuilding.race!=Egypt)
{
	while (1)
		Sleep(100000);
}

while (1)
{
	// Grain Trade
	if (EnvReadString(set, "Grain Trade") == "researched")
	{
		int food,gold; // convert that food to dold

		food = 10;
		gold = 16;
		if (set.food >= food)
		{
			set.SetFood(set.food - food);
			set.SetGold(set.gold + gold);
			set.GoldConverted(gold);
		}
	}
	// Religious feast
	if (EnvReadString(set, "Religious Feast") == "researched")
	{
		ObjList l;
		int i;
		Unit u;
		int units2heal;
		int health;
		int idx,rnd;

		units2heal = 5;// choose that count units nad heal them with that health
		health     = 30;

		l = set.ObjectsAround("Military");

		// Remove enemies, sentries, and not damaged unit from list
		i = l.count;
		while (i > 0)
		{
			i -= 1;
			u = l[i].AsUnit();
			if (This.IsEnemy(u) || u.health == u.maxhealth) 
				l.Remove(u);	
		}
		// Choose 5 random units and heal them;
		
		rnd = l.count/units2heal;
		if (rnd<=0)// l.count<units2heal=> heal all units
		{
			for (i=0; i<l.count; i+=1)
			{
				u = l[i].AsUnit();
				if (u.IsAlive)
				{
					int damage;
					CreateFeedback("Heal", u);
					damage = u.maxhealth - u.health;
					if (damage>health) 
						damage = health;
					u.Heal(damage);
				}
			}
		}
		else
		{
			for (i=0; i<units2heal; i+=1)
			{
				// choose a unit
				idx = i*rnd + rand(rnd); 
				// heal it
				u = l[idx].AsUnit();
				if (u.IsAlive)
				{
					int damage;
					CreateFeedback("Heal", u);
					damage = u.maxhealth - u.health;
					if (damage>health) 
						damage = health;
					u.Heal(damage);
				}
			}
		}
	}
	Sleep(2000);
}
